package com.linwen.tips.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class DataFilter {

    public static List<Integer> filterPositiveNumbers(List<Integer> numbers) {
        List<Integer> result = new ArrayList<>();
        for (Integer number : numbers) {
            if (number > 0) {
                result.add(number);
            }
        }
        return result;
    }

    public static List<Integer> filterEvenNumbers(List<Integer> numbers) {
        List<Integer> result = new ArrayList<>();
        for (Integer number : numbers) {
            if (number % 2 == 0) {
                result.add(number);
            }
        }
        return result;
    }

    public static List<Integer> filterNumbers(List<Integer> numbers, Predicate<Integer> predicate) {
        List<Integer> result = new ArrayList<>();
        for (Integer number : numbers) {
            if (predicate.test(number)) {
                result.add(number);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<Integer> integers = new ArrayList<>();
        integers.add(-2);
        integers.add(-1);
        integers.add(0);
        integers.add(1);
        integers.add(2);
        integers.add(4);
        integers.add(5);
        List<Integer> result1 = filterEvenNumbers(filterPositiveNumbers(integers));
        System.out.println(result1);

        List<Integer> result2 = filterNumbers(filterNumbers(integers, integer -> integer > 0), integer -> integer % 2 == 0);
        System.out.println(result2);
    }


}
